package game.block.trap;

import game.block.Locator;
import game.block.command.Command;
import game.block.command.IceMoveCommand;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.game.Board;
import game.block.tile.Tile;
import game.block.turn.Turn;

import com.badlogic.gdx.math.Vector2;

public class Ice extends TrapEntity {

    public Ice(Vector2 pos, Tile tile) {
        super(pos, tile, ICE, TRAPTYPE.ICE);
    }

    @Override
    public Command advance() {
        return null;
    }

    @Override
    public void applyEffect(MovableEntity block, Board gameBoard, Turn currentTurn, Tile tileMovedTo) {
        if (!block.counterEffect(this)) {
            Tile next = determineVelocity(block, gameBoard);
            if (next != null) {
                Turn turn=new Turn();
                Locator.getAudio().playSound(SOUND.ICE.getValue());
                turn.addCommand(new IceMoveCommand(block, next, currentTile, gameBoard, turn));
                gameBoard.getQueuedTurns().add(turn);
            }
        }
    }

    public Tile determineVelocity(MovableEntity block, Board board) {
        int cPos = (int) (currentTile.getPosition().x / TILE_SIZE);
        int rPos = (int) (currentTile.getPosition().y / TILE_SIZE);
        int arrayPos = (board.getCols() - 1 - rPos) * board.getRows() + cPos;

        if (block.getCurrentTile().getPosition().x < currentTile.getPosition().x) { //moving right
            if (arrayPos % board.getRows() != board.getRows() - 1) {
                Tile tile = board.getTiles().get(arrayPos + 1);
                if (!tile.containsBlock() && !tile.containsWall()) {
                    return tile;
                }
            }
        } else if (block.getCurrentTile().getPosition().x > currentTile.getPosition().x) { //moving left
            if (arrayPos % board.getRows() != 0) {
                Tile tile = board.getTiles().get(arrayPos - 1);
                if (!tile.containsBlock() && !tile.containsWall()) {
                    return tile;
                }
            }
        } else if (block.getCurrentTile().getPosition().y < currentTile.getPosition().y) { //moving up
            if (arrayPos - board.getRows() > 0) {
                Tile tile = board.getTiles().get(arrayPos - board.getRows());
                if (!tile.containsBlock() && !tile.containsWall()) {
                    return tile;
                }
            }
        } else if (block.getCurrentTile().getPosition().y > currentTile.getPosition().y) {  //moving down
            if (arrayPos + board.getRows() < board.getTiles().size()) {
                Tile tile = board.getTiles().get(arrayPos + board.getRows());
                if (!tile.containsBlock() && !tile.containsWall()) {
                    return tile;
                }
            }
        }
        return null;
    }

    @Override
    public Command retract() {
        return null;
    }

    @Override
	public void setInfo() {
		info="Name:  Ice Trap\n\nFunction:  Slides blocks to the next tile. Does not affect sandstone block";
	}
}
